<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class materiaModel extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }
    
     public function getAll(){
         $query = $this->db->query("SELECT  c.id, c.materia, n.nivel from "
                 . "materias c, nivel_materias n where c.nivel = n.id ORDER BY c.materia");
         return $query;
    }
    
    
    public function getLevels(){       
        $query = $this->db->get('nivel_materias');
        return $query->result();
    }
    
   public function getByLevel($levelId){
        $this->db->where('nivel', $levelId);
        $query = $this->db->get('materias');
        return $query->result();
   }
   
   public function getWithProfesorByModel($levelId){
       $q = "SELECT  c.id, c.materia, n.nivel from "
                 . "materias c, nivel_materias n where c.nivel = n.id AND c.nivel = $levelId "
               . "AND 0 < (select count(1) from materias_profesores mp where mp.materia = c.id) ORDER BY c.materia";
       
       $query = $this->db->query($q);
         return $query->result();
   }
    
    public function get($id){
        $this->db->where('id', $id);
        $query = $this->db->get('materias');
        $result = $query->result();
        return $result[0];
    }
   
}